var util = require('../../utils/util.js')
let play=true;
let work={};
const app=getApp();
Page({
  data: {
    Ctext:'',
    Czan:'',
    Ctime:'',
    Wid:'',
    Uid:'',
    commentData:"",
    Wfjudge:'',
    Wtime:'',
    Cfjudge:'',
    CUtime:'',
    CjudgeData:'',
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    interval: 2000,
    duration: 500,
    display:'none',
    isFocus:true,
    isliked:true,
    display1:'none',
    iscri_liked:true,
    heartcri_num:957,
    img : '/images/爱心.png',
    criimg : '/images/爱心.png',
    detailnum:"",
    myId:wx.getStorageSync('Uid')
  },
  onLoad: function (options) {
    this.data.detailnum = options.Wid
    this.getDetailData();
    this.getCommentData();
  },
  //在输入框评论、点击回车图片添加评论
  formSubmit:function(e){
    var that = this;
    console.log(e.detail.value.pinglun)
    //获取当前时间
    var dayTime = util.formatTime(new Date());
    // console.log(dayTime)
    var time1 = dayTime.split(' ')
    var time2 = time1[0].split('/')
    var time3 = time2[0]+'-'+time2[1]+'-'+time2[2]
    this.setData({
      Ctime:time3
    })
    console.log(that.data.Ctime)
    // console.log(time3)

    if(e.detail.value.pinglun == ''){
      wx.showModal({
        title:'提示',
        content:'你还没有发布评论呢~',
        success: function(res){
          if(res.confirm){
            console.log('点击确定了')
          }else if(res.cancel){
            console.log('点击取消了')
            return false;
          }
        }
      })
    }else{
      wx.request({
        url:`${app.globalData.url}/addComment/comment`,
        method:'post',
        // dataType:'json',
        data:{
          //作品id
          Wid:that.data.detailnum,
          //用户id
          Uid:that.data.myId,
          //评论时间
          Ctime:that.data.Ctime,
          //评论点赞量
          Czan:0,
          //评论的内容
          Ctext:e.detail.value.pinglun
        },
        header:{
          'content-type':'application/json'
        },
        success:(res)=>{
          //注意that
          //清理输入框内容
          that.setData({
            Ctext:'',
          })
          that.getCommentData();
        },
        fail:function(res){
          console.log('失败！')
        }
      })
    }
  },
  //获取评论区数据
  getCommentData:function(){
    let self = this;
    wx.request({
      url: `${app.globalData.url}/community/lookComment`, 
      data: {
        detailnum:self.data.detailnum,
        Uid:wx.getStorageSync('Uid'),
        btn:'work'
      },
      method:'GET',
      header: {
        'content-type': 'application/json' 
      },
      success: function (res) {
        console.log(res.data)
        let result = res.data;
        if (result){
          self.setData({
            commentDetail:result
          })
        }
      }
    })
  },
  getDetailData:function(){
    let self = this;
    console.log(self.data.detailnum);
    wx.request({
      url: `${app.globalData.url}/community/detail`, 
      data: {
        detailnum:self.data.detailnum,
        Uid:wx.getStorageSync('Uid')
      },
      method:'GET',
      header: {
        'content-type': 'application/json' 
      },
      success: function (res) {
        console.log(res.data)
        let result = res.data;
        if (result){
          if(result[0].Astate=='已关注'){
            self.setData({
              petsDetail:result,
              bgcolor:'lightgray',
              color:'blck'
            })
          }else{
            self.setData({
              petsDetail:result,
              bgcolor:'#ff986c',
              color:'white'
            })
          }
          
        }
      }
    })
  },  
  //作品点赞设置
  heartadd: function (e){
    //获取当前时间
    var dayTime1 = util.formatTime(new Date());
    // console.log(dayTime)
    var time1 = dayTime1.split(' ')
    var time2 = time1[0].split('/')
    var time3 = time2[0]+'-'+time2[1]+'-'+time2[2]
    wx.request({
      url:`${app.globalData.url}/addWorksPraise/`,
      method:'POST',
      data:{
        img:e.currentTarget.dataset.img=='https://s3.bmp.ovh/imgs/2021/11/8f8308b8016421b4.png'?'https://s3.bmp.ovh/imgs/2021/11/2d7cde86aa7b7457.png':'https://s3.bmp.ovh/imgs/2021/11/8f8308b8016421b4.png',
        Uid:wx.getStorageSync('Uid'),
        Wid:e.currentTarget.id,
        Wtime:time3
      },
      success:res=>{
        this.getDetailData();
        this.getCommentData();
      }
    })
   },
   //评论点赞
  criadd:function(e){
    let time=new Date()
    let year = time.getFullYear()
    let month = time.getMonth() + 1
    let date = time.getDate()
    let hours = time.getHours()
    let minute = time.getMinutes()
    let second = time.getSeconds()
    if (month < 10) { month = '0' + month }
    if (date < 10) { date = '0' + date }
    if (hours < 10) { hours = '0' + hours }
    if (minute < 10) { minute = '0' + minute }
    if (second < 10) { second = '0' + second }
    date=year + '-' + month + '-' + date + ' ' + hours + ':' + minute + ':' + second
    let item=e.currentTarget.dataset.set
    wx.request({
      url:`${app.globalData.url}/addCommentPraise/`,
      method:'POST',
      data:{
        Ctime:date,
        Uid:wx.getStorageSync('Uid'),
        Cid:e.currentTarget.dataset.set.Cid,
        img:e.currentTarget.dataset.set.img=='https://s3.bmp.ovh/imgs/2021/11/8f8308b8016421b4.png'?'https://s3.bmp.ovh/imgs/2021/11/2d7cde86aa7b7457.png':'https://s3.bmp.ovh/imgs/2021/11/8f8308b8016421b4.png'
      },
      success:res=>{
        this.getDetailData();
        this.getCommentData();
        if(!play){
          this.getAstate(item)
        }
      }
    })
  },
  //关注
  already_focus: function (e){
    let Times;
    console.log(this.data.detailnum)
    let time=new Date()
    let year = time.getFullYear()
    let month = time.getMonth() + 1
    let date = time.getDate()
    let hours = time.getHours()
    let minute = time.getMinutes()
    let second = time.getSeconds()
    if (month < 10) { month = '0' + month }
    if (date < 10) { date = '0' + date }
    if (hours < 10) { hours = '0' + hours }
    if (minute < 10) { minute = '0' + minute }
    if (second < 10) { second = '0' + second }
    Times=year + '-' + month + '-' + date + ' ' + hours + ':' + minute + ':' + second
    if(e.currentTarget.dataset.id.Uid!=wx.getStorageSync('Uid')){
      wx.request({
        url: `${app.globalData.url}/community/detail`, 
        method:'POST',
        data: {
          detailnum:this.data.detailnum,
          Uid:wx.getStorageSync('Uid'),
          Astate:e.currentTarget.dataset.id.Astate=='+关注'?'已关注':'+关注',
          Time:Times
        },
        header: {
          'content-type': 'application/json' 
        },
        success: res=>{
          this.getDetailData();
          this.getCommentData();
        }
      })
    }else{
      wx.showModal({
        title:'提示',
        content:'不能关注自己',
        success: function(res){
          if(res.confirm){
            console.log('点击确定了')
          }else if(res.cancel){
            console.log('点击取消了')
            return false;
          }
        }
      })
    }
  },
  //转发作品
  retran:function(e){
    let work=e.currentTarget.dataset.ite;
    let time1= util.formatTime(new Date());
    // console.log(dayTime)
    let time2 =time1.split(' ')
    let time3 = time2[0].split('/')
    let time4 = time3[0]+'-'+time3[1]+'-'+time3[2]
    wx.getLocation({
      type:'wgs84',
      success:function(res){
        console.log(res)
        var latitude = res.latitude
        var longitude = res.longitude
        wx.chooseLocation({
          latitude:latitude,
          longitude:longitude,
          success:res=>{
              wx.request({
                url:`${app.globalData.url}/addWorks/addWorks`,
                data:{
                  //获取标题数据
                  Uid:wx.getStorageSync('Uid'),
                  Wtitle:'转发自@'+work.Uname+' '+work.Wtitle,
                  //获取内容数据
                  Wintro:work.Wintro,
                  //获取选择星球数据
                  Wtag:work.Wtag,
                  Wimage:work.Wimage,
                  Wzan:0,
                  Wtime:time4,
                  Wcity:res.address+' '+res.name
                },
                method:'post',
                header:{
                  'content-type':'application/json'
                },
                success:res=>{
                  wx.showModal({
                    title:'提示',
                    content:'转发成功',
                    success: function(res){
                      if(res.confirm){
                        console.log('点击确定了')
                      }else if(res.cancel){
                        console.log('点击取消了')
                        return false;
                      }
                    }
                  })
                }
                })
          },
          fail:function(){
            console.log('获取地理位置失败!')
          }
        })
      },
      fail:function(){
        console.log('fail')
      }
    })
  },
  resCom:function(e){
    work=e.currentTarget.dataset.ite
    this.setData({
        display1:'block',
        id:work.Cid
    })
},
  formResubmit:function(e){
    console.log(work)
    let time=new Date()
    let year = time.getFullYear()
    let month = time.getMonth() + 1
    let date = time.getDate()
    let hours = time.getHours()
    let minute = time.getMinutes()
    let second = time.getSeconds()
    if (month < 10) { month = '0' + month }
    if (date < 10) { date = '0' + date }
    if (hours < 10) { hours = '0' + hours }
    if (minute < 10) { minute = '0' + minute }
    if (second < 10) { second = '0' + second }
    date=year + '-' + month + '-' + date + ' ' + hours + ':' + minute + ':' + second
    wx.request({
        url:`${app.globalData.url}/addComment/comment`,
        method:'POST',
        data:{
            Wid:work.Cid,
            Uid:wx.getStorageSync('Uid'),
            Ctime:date,
            Czan:0,
            Ctext:e.detail.value.repinglun
        },
        header:{
            'content-type':'application/json'
        },
        success:(res)=>{
          e.detail.value.repinglun=''
          this.setData({
              display1:'none'
          })
          this.getDetailData();
          this.getCommentData();
        },
        fail:function(res){
        console.log('失败！')
        }
    })
},
getAstate:function(e){
  console.log(e)
  wx.request({
    url: `${app.globalData.url}/community/lookComment`, 
    data: {
      detailnum:e.Wid,
      btn:'comment',
      Uid:wx.getStorageSync('Uid')
    },
    method:'GET',
    header: {
      'content-type': 'application/json' 
    },
    success: res=> {
      console.log(res.data)
      this.setData({
        recommentDetail:res.data
      })
    }
  })
},
  getRecomment:function(e){
    play=!play
    if(play){
      this.setData({
        display:'none'
      })
    }else{
      this.setData({
        display:'block'
      })
    }
    wx.request({
      url: `${app.globalData.url}/community/lookComment`, 
      data: {
        detailnum:e.currentTarget.dataset.set.Cid,
        btn:'comment',
        Uid:wx.getStorageSync('Uid')
      },
      method:'GET',
      header: {
        'content-type': 'application/json' 
      },
      success: res=> {
        console.log(res.data)
        this.setData({
          recommentDetail:res.data
        })
      }
    })
  },
  onShow: function () {
    this.getDetailData();
    this.getCommentData();
  },
  changeTalk:function(e){
    let bid=e.currentTarget.id;
    let bname=e.currentTarget.dataset.bname
    wx.navigateTo({
      url:'/pages/me-talking/me-talking?Uid='+wx.getStorageSync('Uid')+'&Bid='+bid+'&Bname='+bname
    })
  },
  jumpPage:function(e){
    let bid=e.currentTarget.id;
    let uid=wx.getStorageSync('Uid')
    console.log(uid)
    wx.navigateTo({
        url:'/pages/me-homepage/me-homepage?Uid='+uid+'&Bid='+bid
    })
  }
})